package br.com.light.adapter.envio.consulta.ws;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.DOMOutputter;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;


public class MappingEnvioConsultaSPWS implements StreamTransformation {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 * @throws StreamTransformationException 
	 */
	public static void main(String[] args) throws FileNotFoundException, StreamTransformationException {
		File file = new File("D:/documentos/Complex/Light/PedidoConsultaNFe.xml");
		InputStream in = new FileInputStream(file);
		
		File fileOut = new File("D:/documentos/Complex/Light/PedidoConsultaNFeWS.xml");
		OutputStream out = new FileOutputStream(fileOut);
		new MappingEnvioConsultaSPWS().execute(in, out);

	}


	public void setParameter(Map param) {
		// TODO Auto-generated method stub
		
	}
	
	public void execute(InputStream in, OutputStream out)
			throws StreamTransformationException {

		try  {
			
			SAXBuilder sb = new SAXBuilder();
			Document msgDoc = sb.build(in);
			
			Element root = msgDoc.getRootElement();
			List elements = root.getChildren();
			Iterator iter = elements.iterator();
			
			
			Format format = Format.getPrettyFormat();
			
			XMLOutputter output = new XMLOutputter(format);
		
			/*			StringBuffer buffer = new StringBuffer();
			while(iter.hasNext()){
				Element elemRoot = (Element)iter.next();
				String tags = output.outputString(elemRoot);
				buffer.append(tags);
				System.out.println(tags);
			}*/
				
			String xmlIn = output.outputString(msgDoc);
			
			Element header = new Element("ConsultaNFeRequest", "ns0", "http://www.prefeitura.sp.gov.br/nfe");
			Element schema = new Element("VersaoSchema", "ns0", "http://www.prefeitura.sp.gov.br/nfe");
			Element msgXML = new Element("MensagemXML", "ns0", "http://www.prefeitura.sp.gov.br/nfe");
			
			schema.setText("1");
			
			header.addContent(schema);
			header.addContent(msgXML);
			//msgXML.setText(new String (buffer.toString().getBytes(), "UTF-8"));
			msgXML.setText(xmlIn);
			
			Document jDOC = new Document(header);
			System.out.println(output.outputString(jDOC));
			
			DOMOutputter outputter = new DOMOutputter();
			org.w3c.dom.Document doc = outputter.output(jDOC);
			
			TransformerFactory tf=TransformerFactory.newInstance();
			Transformer transform=tf.newTransformer();
			transform.transform(new DOMSource(doc), new StreamResult(out));
			
			
		}catch (IOException e){
			e.printStackTrace();
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}	
	

}
